package com.ruoyi.weiriya.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.weiriya.domain.WMessage;
import com.ruoyi.weiriya.service.IWMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.weiriya.mapper.WLeaveWordMapper;
import com.ruoyi.weiriya.domain.WLeaveWord;
import com.ruoyi.weiriya.service.IWLeaveWordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户反馈管理Service业务层处理
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Service
public class WLeaveWordServiceImpl extends ServiceImpl<WLeaveWordMapper, WLeaveWord> implements IWLeaveWordService
{
    @Autowired
    private WLeaveWordMapper leaveWordMapper;

    @Autowired
    private IWMessageService messageService;

    @Override
    public List<WLeaveWord> getList(WLeaveWord wLeaveWord) {
        return leaveWordMapper.getList(wLeaveWord.queryWrapper());
    }

    @Override
    public WLeaveWord getWLeaveWordById(String id) {
        return leaveWordMapper.getWLeaveWordById(id);
    }

    @Transactional
    @Override
    public boolean updateWLeaveWordById(WLeaveWord wLeaveWord) {
        if(StringUtils.isBlank(wLeaveWord.getReplyContent())){
            throw new ServiceException("请输入回复内容！");
        }
        if(wLeaveWord.getReplyContent().length() > 1000){
            throw new ServiceException("回复内容不能超过1000个字符！");
        }

        wLeaveWord.setState(1);
        wLeaveWord.setReplyUserId(SecurityUtils.getUserId().toString());
        WLeaveWord byId = this.getById(wLeaveWord.getId());

        if(byId.getState() < 3){
            throw new ServiceException("不可重复回复！");
        }

        byId.setState(1);
        byId.setReplyUserId(SecurityUtils.getUserId().toString());
        byId.setReplyContent(wLeaveWord.getReplyContent());

        WMessage wMessage = new WMessage(byId);
        messageService.save(wMessage);
        return this.updateById(wLeaveWord);
    }
}
